<?php

/**
 * mailing_list actions.
 *
 * @package    Terapia Vital
 * @subpackage mailing_list
 * @author     Foxsis
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class mailing_listActions extends sfActionsFoxsis
{
  public function executeIndex(sfWebRequest $request)
  {
    return $this->forward('mailing_list', 'list');
  }

	public function executeGrid()
	{
		//Recupero todas as informações necessárias para trabalhar com o grid
		$this->getRequestDataTable();
		$this->data					= MailingListTable::consultar($this->filter, $this->order);
		$this->total_data			= count($this->data);
		$this->data					= $this->paginadorDataTable($this->data, $this->page_num, parent::LIMIT_PAGE_DATA_TABLE);

		sfLoader::loadHelpers(array('Url', 'Tag'));

	    foreach($this->data as $key => &$r)
	    {
			$actions = array(
				'edit'	 => link_to(
	    			' ',
					url_for($this->getModuleName().'/edit?id_mailing_list='.$r['id_mailing_list']),
					array( 'class' => 'icoEditar' )
    			),
				'delete' => link_to(
	    			' ',
    				url_for($this->getModuleName().'/delete?id_mailing_list='.$r['id_mailing_list']),
    				array( 'class' => 'icoExcluir delete' )
    			)
    		);
			$r['act'] = implode(' ', $actions);
    	}

		$this->setJSONDataTable($this->data, $this->colunas, $this->total_data);
		return sfView::NONE;
	}


    public function executeList()
    {
        $this->form = new MailingListForm();
    	$this->colunas	= array(
          	$this->form['id_mailing_list']->renderLabel() => "id_mailing_list",
      		$this->form['id_mailing_group']->renderLabel() => "id_mailing_group",
      		$this->form['ds_nome_list']->renderLabel() => "ds_nome_list",
      		$this->form['ds_email_list']->renderLabel() => "ds_email_list",
      		$this->form['st_mailing_list']->renderLabel() => "st_mailing_list",
      		$this->form['st_envio_list']->renderLabel() => "st_envio_list",
      		$this->form['dt_envio_list']->renderLabel() => "dt_envio_list",
			"Ações"	=> "act"
		);

        $this->filter = $this->_getFilter();

        $this->setTemplate('index');
    }

    public function executeFilter()
	{
	    $filter = new Filtro(
			$this->getRequestParameter(sfActionsFoxsis::FILTER_SESSION_USER_ATTRIBUTE)
		);

		$filter->setCulture($this->getUser()->getCulture());
		$this->getUser()->setAttribute('pg_num',1,$this->getModuleName());

		$this->getUser()->setAttribute(
			sfActionsFoxsis::FILTER_SESSION_USER_ATTRIBUTE,
			serialize($filter),
			$this->getModuleName()
		);

		return $this->forward($this->getModuleName(), '/list');
	}

  public function executeShow(sfWebRequest $request)
  {
    $this->mailing_list = Doctrine::getTable('MailingList')->find(array($request->getParameter('id_mailing_list')));
    $this->forward404Unless($this->mailing_list);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new MailingListForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new MailingListForm();

    $this->processForm($request, $this->form);

    $this->forward($this->getModuleName(), 'new');
  }

  public function validateCreate()
  {
  	return true;
  }

  public function handleErrorCreate()
  {
    return $this->forward($this->getModuleName(), 'new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($mailing_list = Doctrine::getTable('MailingList')->find(array($request->getParameter('id_mailing_list'))), sprintf('Object mailing_list does not exist (%s).', $request->getParameter('id_mailing_list')));
    $this->form = new MailingListForm($mailing_list);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($mailing_list = Doctrine::getTable('MailingList')->find(array($request->getParameter('id_mailing_list'))), sprintf('Object mailing_list does not exist (%s).', $request->getParameter('id_mailing_list')));
    $this->form = new MailingListForm($mailing_list);

    $this->processForm($request, $this->form);

    $this->forward($this->getModuleName(), 'edit');
  }

  public function validateUpdate()
  {
  	return true;
  }

  public function handleErrorUpdate()
  {
    return $this->forward($this->getModuleName(), 'edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    try
    {
    $request->checkCSRFProtection();

    $this->forward404Unless($mailing_list = Doctrine::getTable('MailingList')->find(array($request->getParameter('id_mailing_list'))), sprintf('Object mailing_list does not exist (%s).', $request->getParameter('id_mailing_list')));
    $mailing_list->delete();

    $this->redirect('mailing_list/index');
    }
    catch(Excepiton $e)
	{
		$this->getUser()->setFlash('erro', $e->getMessage());
    	$this->redirect('mailing_list/index');
	}

  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
  	try
  	{
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $mailing_list = $form->save();
	  $this->getUser()->setFlash('mensagem', 'Dados salvos com sucesso.');
      $this->redirect('mailing_list/edit?id_mailing_list='.$mailing_list->getIdMailingList());
    }
    else
    {
    throw new Exception($form->getErrorSchema());
    }
    }
    catch(Exception $e)
	{
		$this->getUser()->setFlash('erro', $e->getMessage());
	}
  }
}
